package com.vincent.sciinfo.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vincent.sciinfo.pojo.*;
import com.vincent.sciinfo.service.*;
import com.vincent.sciinfo.utils.StatusCode;
import com.vincent.sciinfo.utils.StatusMessage;
import com.vincent.sciinfo.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherServer;

    @Autowired
    private MessageService messageServer;

    @Autowired
    private PaperService paperServer;

    @Autowired
    private PatentService patentServer;

    @Autowired
    private ProjectService projectServer;

    @Autowired
    private MeetingService meetingServer;

    @Autowired
    private TakeMeetingService takeMeetingServer;

    @Autowired
    private UserService userServer;

    //通过id获得教师信息
    @RequestMapping(value = "/getTeacherInfor")
    public TSMResult getTeacherInfor(@RequestBody TSMResult tsmResult, @RequestParam("teacherId") String teacherId) {
        List<Teacher> teachers = teacherServer.selectTeacherById(teacherId);
        if (teachers.size() == 0) {
            tsmResult.setMessage("查询失败.");
            tsmResult.setData(null);
            return tsmResult;
        } else {
            tsmResult.setMessage("教师信息查询成功！");
            tsmResult.setData(teachers.get(0));
        }
        return tsmResult;
    }

    //查询教师发布的公告
    @RequestMapping("/sel_sys_msg")
    @ResponseBody
    public TSMResult selectSystemMsg() {
        TSMResult tsmResult = new TSMResult();
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        TSMResult messageTSMResult = messageServer.selectSystemMsg();
        List<Message> list = JSON.parseObject(JSON.toJSONString(messageTSMResult.getData()), List.class);
        tsmResult.setData(list);
        return tsmResult;
    }

    //查询管理员发给教师的消息
    @RequestMapping("/sel_personal_msg")
    public TSMResult selectPersonalMsg(@RequestParam("teacherId") String teacherId) {
        TSMResult tsmResult = new TSMResult();
        TSMResult messageTSMResult = messageServer.selectPersonalMsg(teacherId);
        List<Message> messageList = JSON.parseObject(JSON.toJSONString(messageTSMResult.getData()), List.class);
        tsmResult.setCode(StatusCode.SUCCESS);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setData(messageList);
        return tsmResult;
    }

    //根据id查询教师的论文
    @RequestMapping("/show_paper")
    public TSMResult showPaper(
            String Id,
            int ope,
            @RequestParam(value = "pn", defaultValue = "1") int pn,
            @RequestParam(value = "pageSize") int pageSize) {
        //ope 代表论文的状态
        // 如果是1代表已经审核通过 如果是0代表已提交等待审核通过
        //如果是3代表已被下架
        TSMResult tsmResult = new TSMResult();
        PageHelper.startPage(pn, pageSize);
        PageInfo pageInfo = paperServer.showPaper(Id, ope, pn, pageSize);
        tsmResult.setData(pageInfo);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        return tsmResult;
    }

    //新增论文
    @RequestMapping("/add_paper")
    public TSMResult addPaper(@RequestBody(required = false) Paper paper) {
        TSMResult tsmResult = new TSMResult();
        paper.setResultuuid(UuidUtil.getUuid(6));
        paper.setCheckflag(0);
        paper.setTeacherid(paper.getTeacherid());
        //调paper服务
        int code = paperServer.insertPaper(paper);
        tsmResult.setData(code);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        return tsmResult;
    }

    //更改教师信息 如果是空的 那么管理员那边需要创建一个
    @RequestMapping(value = "/updateTeacherInfor")
    public TSMResult updateTeacherInfor(@RequestBody(required = false) Teacher teacher) {
        TSMResult tsmResult = new TSMResult();
        //当管理员操作新增教师 进入函数时 需要新增教师
        if (teacher.getTeacherid() == null) {
            teacher.setTeacherid(UuidUtil.getUuid(6));
            teacher.setTeacherstatu(1);
            int data = teacherServer.insertTeacher(teacher);

            //为该教师创建用户账号
            int userData = userServer.insertUser(teacher);
            tsmResult.setMessage((data == 0) ? "教师创建失败！" : "教师创建成功！");
            tsmResult.setData(data);
            return tsmResult;
        }
        teacherServer.updateTeacher(teacher);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        return tsmResult;
    }

    //根据id查询教师的专利
    @RequestMapping("/show_patent")
    public TSMResult showPatent(@RequestParam String Id, @RequestParam int ope, @RequestParam(value = "pn", defaultValue = "1") int pn) {
        TSMResult tsmResult = new TSMResult();
        PageHelper.startPage(pn, 5);
        PageInfo pageInfo = patentServer.showPatent(Id, ope, pn);
        tsmResult.setData(pageInfo);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        return tsmResult;
    }

    //新增专利
    @RequestMapping("/add_patent")
    public TSMResult addPatent(@RequestBody(required = false) Patent patent) {
        TSMResult tsmResult = new TSMResult();
        patent.setResultuuid(UuidUtil.getUuid(6));
        patent.setCheckflag(0);
        int code = patentServer.insertPatent(patent);
        tsmResult.setData(code);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        return tsmResult;
    }

    //根据id查询教师的项目
    @RequestMapping("/show_project")
    public TSMResult showProject(String Id, int ope, @RequestParam(value = "pn", defaultValue = "1") int pn) {
        TSMResult tsmResult = new TSMResult();
        PageHelper.startPage(pn, 5);
        List<Project> list = projectServer.selectProject(ope, Id);
        PageInfo page = new PageInfo(list, 5);
        tsmResult.setData(page);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        return tsmResult;
    }

    //新增项目
    @RequestMapping("/add_project")
    public TSMResult addProject(@RequestBody(required = false) Project project) {
        //首先查询该项目 是否已经存在于数据库中
        TSMResult tsmResult = new TSMResult();
        Project projectSelect = projectServer.selectOneProject(project);
        if (projectSelect != null) {
            return new TSMResult(StatusCode.FAILED, StatusMessage.PROJECT_NAME_EXISTED, 0);
        } else {
            int result = projectServer.insertProject(project);
            project.setResultuuid(UuidUtil.getUuid(6));
            project.setCheckflag(0);
            tsmResult.setMessage(result > 0 ? StatusMessage.SUCCESS : StatusMessage.FAILED);
            tsmResult.setCode(result > 0 ? StatusCode.SUCCESS : StatusCode.FAILED);
            tsmResult.setData(result);
        }
        return tsmResult;
    }

    //查询所有会议
    @RequestMapping("/show_meeting")
    public Map showAllTheMeeting() {
        List<Meeting> list = meetingServer.selectAllMeeting();
        Map<String, Object> map = new HashMap<>();
        List<Meeting> meetingList = new ArrayList<>();
        //根据每一个会议编号 去查询对应的人员
        for (Meeting meeting : list) {
            //查出来的是多个数据
            List<TakeMeeting> takeMeetingList = takeMeetingServer.selectByMeetingId(meeting.getMeetingid()); //列中的List
            meeting.setTakeMeetingList(takeMeetingList);
            meetingList.add(meeting);
        }
        map.put("meetingList", meetingList);
        map.put("tsmResult", new TSMResult(200, "查询成功", list));
        return map;
    }

    //参加会议
    @RequestMapping("/takeMeeting")
    public TSMResult takeMeeting(@RequestParam("meetingid") int meetingid, @RequestParam("teacherId") String teacherId) {
        String message = StatusMessage.TAKE_MEETING_SUCCESS;
        int code = StatusCode.SUCCESS;
        //如果返回值为0 则参加失败 如果是2 则已经参加过了
        int data = takeMeetingServer.takeMeeting(meetingid, teacherId);

        if (data == 0) {
            message = StatusMessage.TAKE_MEETING_FAILED;
            code = StatusCode.FAILED;
        } else if (data == 2) {
            message = StatusMessage.HAVE_TAKEN_MEETING;
            code = StatusCode.FAILED;
        }
        return new TSMResult(code, message, data);
    }

    @RequestMapping("/selectAllTeacher")
    public PageInfo selectAllTeacher(@RequestParam Integer pn, @RequestParam Integer pageSize) {
        PageHelper.startPage(pn, pageSize);
        List<Teacher> list = teacherServer.selectAllTeacher();
        PageInfo pageInfo = new PageInfo(list, pageSize);
        return pageInfo;
    }

    @RequestMapping("/updateTeacher")
    public int updateTeacher(@RequestParam String teacherId, @RequestParam int operaCode){
        return teacherServer.updateTeacher(teacherId, operaCode);
    }

    @RequestMapping("/selectTeacherNotLogOut")
    public List<Teacher> selectTeacherNotLogOut(@RequestParam int status){
        return teacherServer.selectTeacherNotLogOut(status);
    }
}
